// pages/detail-search/detail-search.js
import {
  getSearchHot,
  getSerachSuggest,
  getSerachResult
} from '../../services/search'
Page({

  data: {
    keywords: '',
    serachHotWords: [],
    searchSuggestion:[],
     resultSongs: []
  },

  onLoad(options) {
    getSearchHot().then((res) => {
        this.setData({
          serachHotWords: res.result.hots
        })
      }

    )
  },

  onSearchChange(event) {
    
    const keywords=event.detail;
    this.setData({
      keywords
    })
     // 3.判断关键字为空字符的处理逻辑
     if (!keywords.length) {
      this.setData({ searchSuggestion: [], resultSongs: [] })
      // debounceGetSearchSuggest.cancel()
      return
    }
    getSerachSuggest(this.data.keywords).then((res) => {
      // console.log(res)
      this.setData({
        searchSuggestion: res.result.allMatch
      })
    })
  },
  onSearchAction() {
    const keywords = this.data.keywords;
    getSerachResult(keywords).then((res) => {
      // console.log(res)
      this.setData({
        resultSongs : res.result.songs
      })
    })
  },
  handleKeywordItemClick(event) {
    const keywords = event.currentTarget.dataset.value;
    this.setData({
      keywords
    })
    this.onSearchAction()
  }
})